package cn.kgc.controller;

import cn.kgc.pojo.Client;
import cn.kgc.service.ClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ClientController {
    @Autowired
    private ClientService clientService;


    //跳转到用户管理页面
    @GetMapping("/client/ui")
    public  String toClientListUI(){
        return "clientList";
    }

    //跳转到用户新增页面
    @GetMapping(value = "/client/add/ui")
    public  String toClientAddUI(){
        return "clientAdd";
    }

    //跳转到用户新增页面
    @GetMapping(value = "/client/update/ui")
    public  String toClientUpdateUI(){
        return "clientEdit";
    }


    //分页模糊查询用户
    @GetMapping("/client/list")
    @ResponseBody
    public Map<String ,Object> getClientList(String page, String clientName, String limit){
        HashMap<String ,Object> map=new HashMap<>();
        int limi=Integer.parseInt(limit);
        int crrPage=1;
        if (page!=null&&!"".equals(page)){
            crrPage=Integer.parseInt(page);

        }

        crrPage=(crrPage-1)*limi;
        List<Client> clientList=clientService.getClientList(crrPage,clientName,limi);
        System.out.println(clientName);
        int totalPage=clientService.getToalCount(clientName);
                    map.put("code",0);
            map.put("msg","");
            map.put("count",totalPage);
            map.put("data",clientList);
        return map;
    }

    //新增用户
    @PostMapping("/client/add")
    @ResponseBody
    public Map<String ,Object> getClientAdd(Client client){
        HashMap<String ,Object> map=new HashMap<>();
        int clientAdd=clientService.ClientAdd(client);
        map.put("code",0);
        map.put("msg","");
        map.put("data",clientAdd);
        return map;
    }
    //删除用户
    @PostMapping("/client/delete/{clientId}")
    @ResponseBody
    public Map<String ,Object> getClientDelete(@PathVariable("clientId") Integer clientId){
        HashMap<String ,Object> map=new HashMap<>();
        int clientAdd=clientService.ClientDelete(clientId);
        map.put("code",0);
        map.put("msg","");
        map.put("data",clientAdd);
        return map;
    }

    @GetMapping("/client/update/{clientId}")
    public String getClientById(@PathVariable("clientId") Integer clientId, Model model){
        Client client=clientService.getById(clientId);
        model.addAttribute("upclient",client);
        return "clientEdit";
    }

    @RequestMapping(value = "/getClientUpdate",method = RequestMethod.POST)
    @ResponseBody
    public Map<String ,Object> getClientUpdate(Client client){
        HashMap<String ,Object> map=new HashMap<>();
        int clientUpdate=clientService.ClientUpdate(client);
        System.out.println(clientUpdate);
        map.put("code",0);
        map.put("msg","");
        map.put("data",clientUpdate);
        return map;
    }
}
